﻿
var vm = new Vue({
    el: '#app',
    data:function(){
        return{
            pagename:"我的任务",
            common_fn:null,
            userinfo:null,
            teams:null,
            teamusers:null,
            isManger:false,
            mangerTeam:null,
            mainTable_maxHeight:0,
            head:[
                {name:'批次号',property:'properties.mesproductionschedule.lotno','filters':[]},
                {name:'物料编码',property:'properties.plmpart.materialno','filters':[]},
                {name:'产品名称',property:'properties.mesproductionschedule.name','filters':[]},
                {name:'零件名称',property:'properties.plmpart.name','filters':[]},
                {name:'图号',property:'properties.plmpart.identityno','filters':[]},
                {name:'工序号',property:'properties.mespartsemifinished.indexno','filters':[]},
                {name:'工序名',property:'properties.qcpprocess.processname','filters':[]},
                {name:'报工日期',property:'properties.mesreportsheet.reporttime','filters':[]},
                {name:'人员',property:'properties.mesreportsheet.reportor','filters':[]},
                {name:'报工数量',property:'properties.mesreportsheet.realquantity','filters':[]},
                {name:'状态',property:'properties.mesreportsheet.state_name','filters':[]}
            ],
            loading:false,
            param:null,
            data:[],
            searchType:true,
            // 查询参数
            search:{
                lotno:'',
                materialno:'',
                name:'',
                identityno:''
            },
            // 加载条件  默认 待审核
            filter:'我的已完工任务',
            printlist:[],
            // 工时查询
            calc_time:[],
            calc_people:null,
            workTime_result:'',
            worktimeDialog:false,
            printtypeDialog:false,
            //分页-----------------
            // 总数量
            totalnum:0,
            // 总页数
            totalpage:0,
            // 每页数量
            everypagenum:20,
            // 当前页
            currentpage:1,
            //组件信息
            differentDialog:null,
            differentDialogItem:null
        }
    },
    methods:{
        // 获取所有数据
        getData:function(num){
            var self = this;
            self.loading = true;
            self.param['pageNum'] = self.everypagenum;
            self.param['pageIndex'] = self.currentpage;
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.ReportSheetMgr","QueryMyWorkList",self.param,self.userinfo,function(response){
                if(response.resFlag == 0){
                    for(var j=0;j<response.result.items.length;j++){
                        response.result.items[j]['properties'] = {};
                        for(var k=0;k<response.result.items[j].itemObjects.length;k++){
                            response.result.items[j].properties[response.result.items[j].itemObjects[k].type] = response.result.items[j].itemObjects[k].properties;
                            response.result.items[j].properties[response.result.items[j].itemObjects[k].type]['id'] = response.result.items[j].itemObjects[k].id;
                        }
                        response.result.items[j].properties.mesproductionschedule.startdate = response.result.items[j].properties.mesproductionschedule.startdate.substring(0, 10);
                        response.result.items[j].properties.mesproductionschedule.enddate = response.result.items[j].properties.mesproductionschedule.enddate.substring(0, 10);
                        response.result.items[j].properties.mesproductionschedule.lotnum = Number(response.result.items[j].properties.mesproductionschedule.lotnum);
                        response.result.items[j].properties.mesproductionschedule.requisitionquantity = Number(response.result.items[j].properties.mesproductionschedule.requisitionquantity);
                        response.result.items[j].properties.mesproductionschedule.realrequisitionquantity = response.result.items[j].properties.mesproductionschedule.realrequisitionquantity==''?'':Number(response.result.items[j].properties.mesproductionschedule.realrequisitionquantity);          
                        response.result.items[j].properties.mesreportsheet['state_name'] = response.result.items[j].properties.mesreportsheet.state==1?'未开工':
                        response.result.items[j].properties.mesreportsheet.state==2?'已开工':
                        response.result.items[j].properties.mesreportsheet.state==3?'审核中':
                        response.result.items[j].properties.mesreportsheet.state==5?'已完工':'';
                    }
                    self.data = response.result.items;
                    pushFilterData('head',self.head,self.data);
                }else{
                    notify(response.errMsg,'error')
                }
                self.loading = false;
            })
        },
        getCount:function(){
            var self = this;
            var filters = [];
            var param = {
                "filters": [],
                "orderItems": [],
                "queryclasses": ["MesReportSheet", "MesProductionSchedule", "PlmPart", "MesTeam", "SysPerson",
                        "QcpProcess", "MesSemiFinished", "SysSnapshot","MesOrderSheet"],
                "queryFields": [{
                    "classType": "MesPartSemiFinished",
                    "propName": "IndexNo"
                }],
                "objectFormat": false,
                "user": self.userinfo.user,
                "connectionId": 1,
                "appName": "MES"
            }
            if(self.searchType){
                if(self.search.materialno != ''){
                    param.filters.push({
                        "classFlag": 1,
                        "classType": "PlmPart",
                        "propName": "materialno",
                        "op": 7,
                        "propValue":self.search.materialno,
                        "propValue2": "",
                        "logic": 0,
                        "leftSplitter": 0,
                        "rightSplitter": 0,
                        "bAddWhereClauseFlag": true
                    })
                }
                if(self.search.identityno != ''){
                    param.filters.push({
                        "classFlag": 1,
                        "classType": "PlmPart",
                        "propName": "identityno",
                        "op": 7,
                        "propValue":self.search.identityno,
                        "propValue2": "",
                        "logic": 0,
                        "leftSplitter": 0,
                        "rightSplitter": 0,
                        "bAddWhereClauseFlag": true
                    })
                }
                if(self.search.lotno != ''){
                    param.filters.push({
                        "classFlag": 1,
                        "classType": "MesProductionSchedule",
                        "propName": "lotno",
                        "op": 7,
                        "propValue":self.search.lotno,
                        "propValue2": "",
                        "logic": 0,
                        "leftSplitter": 0,
                        "rightSplitter": 0,
                        "bAddWhereClauseFlag": true
                    })
                }
                if(self.search.name != ''){
                    param.filters.push({
                        "classFlag": 1,
                        "classType": "MesProductionSchedule",
                        "propName": "name",
                        "op": 7,
                        "propValue":self.search.name,
                        "propValue2": "",
                        "logic": 0,
                        "leftSplitter": 0,
                        "rightSplitter": 0,
                        "bAddWhereClauseFlag": true
                    })
                }
            }
            if(self.filter == '班组已完工任务'){
                var manager_teamids = [];
                if (!self.teams && self.teams.length > 0) {
                    for (var i = 0; i < self.teams.length; i++) {
                        var team = self.teams[i];
                        if (team.properties.manager.toLocaleLowerCase() == 'yes') {
                            manager_teamids.push(team.id);
                        }
                    }
                }
                if (manager_teamids.length > 0) {
                    for (var i = 0; i < manager_teamids.length; i++) {
                        param.filters.push({
                            "classFlag": 0,
                            "classType": "MesReportSheet",
                            "propName": "Team_OBJID",
                            "op": 4,
                            "propValue": manager_teamids[i] + "",
                            "propValue2": null,
                            "logic": 1,
                            "leftSplitter": i == 0 ? 1 : 0,
                            "rightSplitter": 0,
                            "bAddWhereClauseFlag": true
                        });
                    }
                }
                param.filters.push({
                    "classFlag": 0,
                    "classType": "MesReportSheet",
                    "propName": "Person_OBJID",
                    "op": 4,
                    "propValue": self.userinfo.user.id + "",
                    "propValue2": null,
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": manager_teamids.length > 0 ? 1 : 0,
                    "bAddWhereClauseFlag": true
                });
            }else{
                param.filters.push({
                    "classFlag": 1,
                    "classType": "MesReportSheet",
                    "propName": "Person_OBJID",
                    "op": 4,
                    "propValue": self.userinfo.user.id + "",
                    "propValue2": null,
                    "logic": 0,
                    "leftSplitter": 0,
                    "rightSplitter": 0,
                    "bAddWhereClauseFlag": true
                });
            }
            self.common_fn.invokeFun("Business_QueryReportSheetOneSetCount",param,self.userinfo,function(response){
                if(response.resFlag == 0){
                    self.totalnum = response.result;
                    self.totalpage = Math.ceil(self.totalnum/self.everypagenum);
                    self.param = param;
                    self.currentpage = 1;
                    self.getData(1);
                }else{
                    notify(response.errMsg,'error')
                }
            })
        },
        // 加载上一页或下一页
        handleCurrentChange:function(num){
            var self = this;
            self.currentpage = num;
            self.getData(num);
        },
        // 工时查询
        calcWorkTime:function(){
            var self = this;
            if(self.calc_time.length==0){
                notify_timeOut('请选择时间范围','error');return
            }
            if(self.calc_time[0]>self.calc_time[1]){
                notify_timeOut('开始时间不能大于结束时间','error');return
            }
            var starttime = self.calc_time[0].getFullYear()+'-'+(self.calc_time[0].getMonth()+1)+'-'+self.calc_time[0].getDate();
            var endtime = self.calc_time[1].getFullYear()+'-'+(self.calc_time[1].getMonth()+1)+'-'+self.calc_time[1].getDate();
            
            var param = {
                sql:"select sum((QcpProcess__PrepareHour+QcpProcess__ManufactoryHour)*MesReportSheet__RealQuantity) as result from view_allmesreportsheet where mesreportsheet__person_objid='"+ self.calc_people +"' and mesreportsheet__enddate between '"+ starttime +"' and '"+ endtime +"'",
                appName:"mes"
            }
            self.common_fn.sqlFn(param,function(response){
                if (response.resFlag === 0) {
                    if(response.result && response.result.length>0){
                        self.workTime_result = Number(response.result[0].properties.result);
                    }
                } else {
                    notify(response.errMsg,'error');
                }
            })
        },
        // 打印状态查看
        printtype:function(row){
            var self = this;
            self.tempItem = row;
            self.printlist = [];
            var param = {
                "obj":{
                    id:Number(row.properties.mesreportsheet.id),
                    type:"MesReportSheet",
                    properties:{}
                },
                "user":self.userinfo.user,
                "connectionId": 1,
                "appName": "MES"
            };
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.PrintMgr", "CheckReportSheetPrintState",param,self.userinfo,function(response){
                if(response.resFlag == 0) {
                    self.printlist = response.result;
                    self.printtypeDialog = true;
                }else{
                    notify(response.errMsg,'error');
                }
            })
        },
        // 打印状态查看打印
        print:function(){
            var self = this;
            var param = {
                "printObj"  : {
                    id:Number(self.tempItem.properties.mesreportsheet.id),
                    type:"MesReportSheet",
                    properties:{
                        printType:"我的任务"
                    }
                },
                "strKey":self.tempItem.properties.mesreportsheet.name,
                "description":"网页补打",
                "user": self.userinfo.user,
                "connectionId": 1,
                "appName": "MES"
            };
            self.common_fn.invoke('JridgeMesSTD_AdditionalService.dll', "JridgeMesSTD_AdditionalService.PrintMgr", "CretaePrintRequest", param,self.userinfo,function(response){
                if(response.resFlag != 0) {
                    notify(response.errMsg,'error');
                }else{
                    notify_timeOut('打印成功','success');
                    self.printtypeDialog = false;
                }
            })
        },
        retColor:function(item){
            if(item.column.label == '批次号'){ return 'yellow' }
        },
        cell_click:function(row, column, cell){
            var self = this;
            self.tempItem = row;
            if(column.label=='批次号'){
                self.needLoadComponend();
            }
        },
        needLoadComponend:function(){
            var self = this;
            self.differentDialogItem = self.tempItem;
            self.differentDialog = '';
            window.setTimeout(()=>{
                self.differentDialog = 'detailmsg';
            },200)
        },
    },
    mounted:function(){
        var self = this;
        self.common_fn = new common_fn();
        self.common_fn.init(function(){
            self.common_fn.UserInfo(function(response){
                if(response.user == null){
                    window.location.href = 'login.html?redirect=mywork.html';
                }
                self.userinfo = response;
                self.common_fn.CheckFunction(self.userinfo,'mes','mes','MyWork','','','mywork',function(){})
                self.calc_people = self.userinfo.user.id;
                // 查询所在班组
                self.common_fn.PersonInTeam(self.userinfo.user.id,function(res){
                    if(res.resFlag == 0){
                        self.teams = res.result;
                        // 查询当前登录人是否是某班组的班组长
                        for(var i=0;i<self.teams.length;i++){
                            if(self.teams[i].properties.manager.toLocaleLowerCase() == 'yes'){
                                self.isManger = true;
                                self.mangerTeam = self.teams[i];break 
                            }
                        }
                        //查询班组长所在班组所有成员
                        if(self.isManger){
                            self.common_fn.TeamUsers(self.mangerTeam.id,function(res1){
                                if(res1.resFlag == 0){
                                    self.teamusers = res1.result;
                                }else{
                                    notify(res1.errMsg,'error')
                                }
                            })
                        }
                        self.getCount();
                        self.calc_time[0] = new Date(new Date().setDate(1));
                        self.calc_time[1] = new Date();
                        self.calcWorkTime();
                    }else{
                        notify(response.errMsg,'error')
                    }
                })
            })
        });
    }
})
window.onload =function(){
    if(vm.mainTable_maxHeight!=null ||vm.mainTable_maxHeight!=undefined){
        window.setTimeout(function(){
            vm.mainTable_maxHeight = window.screen.availHeight - document.body.clientHeight -70;
        },100)
    }
}
